//
//  FQWebGoBackAction.m
//  KDFDApp
//
//  Created by 蔡亮 on 2018/1/8.
//  Copyright © 2018年 cailiang. All rights reserved.
//

#import "FQWebGoBackAction.h"
#import "UIView+Additions.h"
#import "FQWKWebResponseEntity.h"
#import "FQBaseWKWebViewEntity.h"
@interface FQWebGoBackAction ()
@property (nonatomic, strong) NSMutableDictionary <NSString *, void(^)(FQWKWebResponseEntity *)>*map;
@property (nonatomic, strong) QCWKWebViewJSRequestEntity *entity;
@property (nonatomic, weak) WKWebView *webView;
@end
@implementation FQWebGoBackAction
-(NSDictionary<NSString *,void (^)(FQWKWebResponseEntity *)> *)map
{
    if (!_map) {
        _map = [@{} mutableCopy];
    }
    return _map;
}
- (void)setWebView:(WKWebView *)webview
{
    _webView = webview;
}
-(void)action:(QCWKWebViewJSRequestEntity *)requestEntity andCallbackBlock:(void (^)(FQWKWebResponseEntity *))response
{
    _entity = requestEntity;
    if (requestEntity&&requestEntity.callback) {
        [self.map setObject:[response copy] forKey:requestEntity.callback];
    }
    [self goback:requestEntity];
}

- (void)goback:(QCWKWebViewJSRequestEntity *)request{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
        [self successResponseWithRequest:request param:@{}];
    }else{
        UIViewController *vc =[UIApplication sharedApplication].windows[0].rootViewController ;
        NSArray * childVCs = [vc.navigationController childViewControllers];
        if (childVCs.count<=1) {
            return;
        }
        NSMutableArray *newChildVCArr = [@[] mutableCopy];
        for (UIViewController *vc in childVCs) {
            UIViewController *tmp = _webView?_webView.viewController:nil;
            if(tmp&&[vc isEqual:tmp]){
                continue;
            }
            [newChildVCArr addObject:vc];
        }
        [vc.navigationController setViewControllers:newChildVCArr];
        [self successResponseWithRequest:request param:@{}];
    }
}
#pragma mark - 成功回调
-(void)successResponseWithRequest:(QCWKWebViewJSRequestEntity *)request param:(NSDictionary *)param{
    FQWKWebResponseEntity *response = [FQWKWebResponseEntity getResponse];
    response.callbackid = request.callback;
    response.data.code = ENUM_Succeed;
    response.data.message = @"succeed";
    response.data.data = param ? : @{};
    [self.map enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, void (^ _Nonnull obj)(FQWKWebResponseEntity *response), BOOL * _Nonnull stop) {
        if (key == request.callback) {
            obj(response);
        }
    }];
}
#pragma mark - 失败回调
-(void)failResponseWithRequest:(QCWKWebViewJSRequestEntity *)request{
    FQWKWebResponseEntity *response = [FQWKWebResponseEntity getResponse];
    response.callbackid = request.callback;
    response.data.code = ENUM_ParamError;
    response.data.message = @"未获取到当前webview";
    response.data.data = @{};
    [self.map enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, void (^ _Nonnull obj)(FQWKWebResponseEntity *response), BOOL * _Nonnull stop) {
        if (key == request.callback) {
            obj(response);
        }
    }];
}

@end

